//
//  AreaTableViewController.m
//  FreeTrip
//
//  Created by 352324497@qq.com on 15/11/24.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "AreaTableViewController.h"
#import "abAndeInModel.h"
#import "AreaTableViewCell.h"
#import "AreaWithShoppinTableViewCell.h"
#import "StrategyController.h"
#import "BookController.h"
#import "ShopController.h"

#import "WeatherViewController.h"

#import "KCMainViewController.h"

@interface AreaTableViewController ()



@end

@implementation AreaTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    self.navigationController.navigationBar.translucent = NO;
    
    self.tableView.separatorStyle = NO;
    abAndeInModel *model = self.modelArray[0];
    
    self.navigationItem.title = model.name_zh_cn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    abAndeInModel *model = self.modelArray[indexPath.row];
    if (model.items_count == 0)
    {
        return [AreaWithShoppinTableViewCell heightForCell];
    }
    else
    {
        return [AreaTableViewCell heightForCell];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    abAndeInModel *model = self.modelArray[indexPath.row];
    if (model.items_count == 0)
    {
        AreaWithShoppinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil)
        {
            cell = [[AreaWithShoppinTableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
        }
        cell.layer.borderWidth = 0.5;
        cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
        cell.model = model;
        [cell.strategyButton addTarget:self action:@selector(strategyButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [cell.bookButton addTarget:self action:@selector(bookButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [cell.mapButton addTarget:self action:@selector(mapButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [cell.toolButton addTarget:self action:@selector(toolButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        return cell;
    }
    else
    {
        AreaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil)
        {
            cell = [[AreaTableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
        }
        cell.layer.borderWidth = 0.5;
        cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
        cell.model = model;
        [cell.strategyButton addTarget:self action:@selector(strategyButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [cell.bookButton addTarget:self action:@selector(bookButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [cell.mapButton addTarget:self action:@selector(mapButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [cell.toolButton addTarget:self action:@selector(toolButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [cell.shoppingButton addTarget:self action:@selector(shoppingButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        return cell;

    }
}

#pragma mark - 攻略点击方法
-(void)strategyButtonAction:(DIYButton *)strategyButton
{
    AreaTableViewCell *cell = (AreaTableViewCell *)strategyButton.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    StrategyController *strategyViewVc = [[StrategyController alloc]init];
    abAndeInModel *model = self.modelArray[indexPath.row];
    strategyViewVc.Id = model.Id;
    strategyViewVc.name = model.name_zh_cn;
    [self.navigationController pushViewController:strategyViewVc animated:YES];
}

#pragma mark - 口袋书点击方法
-(void)bookButtonAction:(DIYButton *)bookButton
{
    AreaTableViewCell *cell = (AreaTableViewCell *)bookButton.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    BookController *bookViewVc = [[BookController alloc]init];
    abAndeInModel *model = self.modelArray[indexPath.row];
    bookViewVc.Id = model.Id;
    [self.navigationController pushViewController:bookViewVc animated:NO];
}

#pragma mark - 地图点击方法
-(void)mapButtonAction:(DIYButton *)mapButton
{
    AreaTableViewCell *cell = (AreaTableViewCell *)mapButton.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    KCMainViewController *KCMainViewVc = [[KCMainViewController alloc]init];
    abAndeInModel *model = self.modelArray[indexPath.row];
    KCMainViewVc.Id = model.Id;
    [self.navigationController pushViewController:KCMainViewVc animated:NO];
}

#pragma mark - 天气点击方法
-(void)toolButtonAction:(DIYButton *)Button
{
    AreaTableViewCell *cell = (AreaTableViewCell *)Button.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    abAndeInModel *model = self.modelArray[indexPath.row];
    WeatherViewController *weaVc = [[WeatherViewController alloc] init];
    weaVc.Id = model.Id;
    [self.navigationController pushViewController:weaVc animated:NO];
    
}

#pragma mark - 购物点击方法
-(void)shoppingButtonAction:(ShoppingButton *)shoppingButton
{
    ShopController *shopViewVc = [[ShopController alloc]initWithStyle:(UITableViewStyleGrouped)];
    AreaTableViewCell *cell = (AreaTableViewCell *)shoppingButton.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    abAndeInModel *model = self.modelArray[indexPath.row];
    shopViewVc.Id = model.Id;
    shopViewVc.name = model.name_zh_cn;
    [self.navigationController pushViewController:shopViewVc animated:YES];
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
